﻿using AvalonDock.Layout;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using SqLiteEditor.Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace SqLiteEditor.Templates
{
    /// <summary>
    /// Interaction logic for TBrowsWindow.xaml
    /// </summary>
    public partial class TBrowsWindow : UserControl
    {
        public TBrowsWindow()
        {
            InitializeComponent();
            SqliteDataGrid.Theme = ExtendedGrid.ExtendedGridControl.ExtendedDataGrid.Themes.Developer;
        }

        public int RowCount
        {
            get { return (int)GetValue(RowCountProperty); }
            set { SetValue(RowCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RowCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RowCountProperty =
            DependencyProperty.Register("RowCount", typeof(int), typeof(TBrowsWindow), new PropertyMetadata(0));


        public void SetFlowDirection(FlowDirection fd)
        {
            SqliteDataGrid.FlowDirection = fd;
        }

        public void SetFontSize(double fontsize)
        {
            SqliteDataGrid.FontSize = fontsize;
        }

        public TBrowsWindow(string query)
            : this()
        {
            InitializeSyntaxHighlighting();

            SqliteDataGrid.ItemsSource = SqliteHelper.ExecutQuery(query).DefaultView;

            RowCount = SqliteDataGrid.Items.Count;

            BindingOperations.SetBinding(this, RowCountProperty, new Binding("Count") { Source = SqliteDataGrid.Items });

        }
       
        private void InitializeSyntaxHighlighting()
        {
            using (Stream s = TStreamHelper.OpenStream("Sql.xshd"))
            {
                using (XmlTextReader reader = new XmlTextReader(s))
                {
                    QueryEdittor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
                }
            }
        }

    }
}
